package helper

import (
	"fmt"
	"strconv"
)

// 30 - 37 为前景色, 40 - 47 为背景色
// 30, 40 ---- 黑色
// 31, 41 ---- 红色
// 32, 42 ---- 绿色
// 33, 43 ---- 黄色
// 34, 44 ---- 蓝色
// 35, 45 ---- 紫色
// 36, 46 ---- 浅蓝色
// 37, 47 ---- 灰白色

// PrintWithColor 打印带颜色的内容 ret : 0 error, 1 success, 2 warning, 3 info, 4 primary
func PrintWithColor(outStr string, ret uint) {
	retColor := 31
	retString := "error"
	midString := "----------"
	switch ret {
	case 1:
		retColor = 32
		retString = "ok"
		midString = "-------------"
	case 2:
		retColor = 33
		retString = "warning"
		midString = "--------"
	case 3:
		retColor = 37
		retString = "info"
		midString = "-----------"
	case 4:
		retColor = 36
		retString = "primary"
		midString = "--------"
	}
	fmt.Printf("[ %c[%d;%d;%dm%s%c[0m ] %s %s\n", 0x1B, 0, 0, retColor, retString, 0x1B, midString, outStr)
}

// PrintWithBgColor 打印带颜色的内容
// color : error - 红色, success - 绿色, warning - 黄色, primary - 浅蓝色, info - 灰白色
func PrintWithBgColor(outStr, color, mark string) {
	m := map[string]int{
		"error":   41,
		"success": 42,
		"warning": 43,
		"primary": 46,
		"info":    47,
	}
	var bgColor int
	foreColor := 37
	if v, ok := m[color]; ok {
		bgColor = v
		if mark == "" {
			mark = color
		}
	} else if bgColor, _ := strconv.Atoi(color); bgColor > 47 || bgColor < 40 {
		bgColor = 47
	}
	if bgColor == 47 {
		foreColor = 30
	}

	fmt.Printf("%c[%d;%dm %s %c[0m %s\n", 0x1B, bgColor, foreColor, mark, 0x1B, outStr)
}
